<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="客户编号" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-select
          v-model="queryParams.grade"
          placeholder="请选择年级"
          clearable
        >
          <el-option
            v-for="item in gradeOpiton"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签约状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择签约状态"
          clearable
        >
          <el-option label="未签约" :value="0" />
          <el-option label="已签约" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择性别"
          clearable
        >
          <el-option label="男" :value="0" />
          <el-option label="女" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="线索客户来源" prop="customerSource">
        <el-select
          v-model="queryParams.customerSource"
          placeholder="请选择线索客户来源"
          clearable
        >
          <el-option
            v-for="item in customerSourceOpiton"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教育顾问" prop="educationlAdviser">
        <el-select
          v-model="queryParams.educationlAdviser"
          placeholder="请选择教育顾问"
          clearable
        >
        </el-select>
      </el-form-item>
      <el-form-item label="所属校区">
        <campusSelect v-model="queryParams.campus" class="form-select" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="assignCustomers">
          分配客户
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="客户编号"
        align="center"
        prop="customerCode"
        min-width="120"
      />
      <el-table-column label="姓名" align="center" prop="customerName" />
      <el-table-column label="年级" align="center" prop="grade">
        <template #default="{ row }">
          <span>{{ formatGrade(row.grade) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属校区" align="center" prop="campusName" />

      <el-table-column label="就读学校" align="center" prop="school" />

      <el-table-column label="签约状态" align="center" prop="status">
        <template #default="{ row }">
          <el-tag type="success" v-if="row.status === 1">已签约</el-tag>
          <el-tag type="info" v-else>未签约</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="教育顾问"
        align="center"
        prop="educationalAdviserName"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="分配客户" :visible.sync="visible">
      <div class="customer-container">
        将已选择{{ selection.length }}位用户分配至
      </div>
      <div>
        <userSelect v-model="educationalAdviser" class="form-select" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="assignCustomersConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import _ from "lodash";
import { getList, assignCustomers } from "@/api/leadManagement/leadClientList";
import campusSelect from "@/components/Select/campusSelect.vue";
import userSelect from "@/components/Select/userSelect.vue";
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 定时任务表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        customerCode: "", //客户编号
        grade: "", //年级
        customerName: "", //姓名
        status: "", //签约状态
        phone: "", //手机号码
        sex: "", //性别
        customerSource: "", //线索客户来源
        educationlAdviser: "", //教育顾问
        campus: "", //所属校区
        pageNum: 1,
        pageSize: 10,
      },
      // 年级选项
      gradeOpiton: [],
      // 线索客户来源选项
      customerSourceOpiton: [],
      // 选中数据
      selection: [],
      visible: false,
      educationalAdviser: "",
    };
  },
  components: {
    userSelect,
    campusSelect,
  },
  created() {
    this.gradeOpiton = [
      { value: 0, label: "小一" },
      { value: 1, label: "小二" },
      { value: 2, label: "小三" },
      { value: 3, label: "小四" },
      { value: 4, label: "小五" },
      { value: 5, label: "小六" },
      { value: 6, label: "初一" },
      { value: 7, label: "初二" },
      { value: 8, label: "初三" },
      { value: 9, label: "高一" },
      { value: 10, label: "高二" },
      { value: 11, label: "高三" },
      { value: 12, label: "高四" },
    ];

    this.customerSourceOpiton = [
      { value: 0, label: "推荐" },
      { value: 1, label: "户外" },
      { value: 2, label: "拉访" },
      { value: 3, label: "平面媒体" },
      { value: 4, label: "渠道" },
      { value: 5, label: "数据库数据" },
      { value: 6, label: "推广" },
      { value: 7, label: "网络" },
      { value: 8, label: "手机短信" },
      { value: 9, label: "直访" },
      { value: 10, label: "呼入" },
      { value: 11, label: "呼出" },
      { value: 12, label: "回访" },
    ];
    this.getList();
  },
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true;
      getList(this.queryParams).then((res) => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jobId: undefined,
        jobName: undefined,
        jobGroup: undefined,
        invokeTarget: undefined,
        cronExpression: undefined,
        misfirePolicy: 1,
        concurrent: 1,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection;
    },

    formatGrade(grade) {
      return _.find(this.gradeOpiton, { value: grade })?.label || "未知年级";
    },

    assignCustomers() {
      if (!this.selection.length) {
        this.$message.warning("请至少选择一位客户");
        return;
      }
      this.visible = true;
    },

    assignCustomersConfirm() {
      const sysCustomerList = _.map(this.selection, (item) => {
        return {
          customerId: item.customerId,
          educationalAdviser: this.educationalAdviser,
        };
      });
      assignCustomers(sysCustomerList).then(() => {
        this.$message.success("分配成功");
        this.visible = false;
        this.selection = [];
        this.getList();
      });
    },
  },
};
</script>
<style scoped lang="scss">
.customer-container {
  margin-bottom: 10px;
  font-size: 18px;
  color: #606266;
}
</style>
